package com.sojson.project.errorpage.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sojson.config.ErrorPageConfig;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.constant.ConstantByMap;
import com.sojson.constant.ConstantServlet;
import com.sojson.result.Result;
import com.sojson.util.StringUtil;
import com.sojson.util.httpclient.HttpClientUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 错误页面控制器
 * 
 * @author liu
 * @Date 2020-07-02 10:40:18
 * @Description
 *
 */
@Api(tags = "错误页面")
@ApiIgnore
@Controller
@RequestMapping("${so.controller.mapping.open:/open}/error")
public class ErrorController {

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    protected String errorPath = "/error/";
    protected String errorPageUrl = ConstantByMap.CONTROLLER_URL_LONG_RANGE_PAGE + errorPath;

    /**
     * 跳转到错误页面
     * 
     * @param code
     * @return
     * @throws Exception 
     */
    @RequestMapping("/page/{code}")
    @ApiOperation("跳转到错误页面")
    public String toErrorPage(@PathVariable("code") String code) throws Exception {
        return errorPath + code;
    }

    /**
     * 跳转到错误页面
     * 
     * @param code
     * @return
     * @throws Exception 
     */
    @RequestMapping("/page/500")
    @ApiOperation("跳转到500错误页面")
    public Object toErrorPage() throws Exception {
        return getMessage();
    }

    /**
     * 跳转到错误页面
     * 
     * @param code
     * @return
     * @throws Exception 
     * @throws ServletException
     */
    @RequestMapping("/page/longRange/{code}")
    @ApiOperation("跳转到远程错误页面")
    public void toErrorPageLongRange(@PathVariable("code") String code) throws Exception {
        HttpClientUtil.redirect(
            new StringBuilder(errorPageUrl).append(code).append(ConstantByMap.LONG_RANGE_PAGE_SUFFIX).toString());
    }

    /**
     * 跳转到错误页面
     * 
     * @param code
     * @return
     * @throws Exception 
     * @throws ServletException
     */
    @RequestMapping("/page/longRange/500")
    @ApiOperation("跳转到远程500错误页面")
    public Object toErrorPageLongRange() throws Exception {
        return getMessage();
    }

    /**
     * 返回错误信息
     * 
     * @param code
     * @return
     * @throws Exception 
     */
    @RequestMapping("/json/{code}")
    @ResponseBody
    @ApiOperation("返回错误信息")
    public Object toErrorJson(@PathVariable("code") String code) throws Exception {
        TransException transException = ErrorPageConfig.TRANS_ERROR_CODE.get(code);
        if (StringUtil.isBlankObject(transException)) {
            return Result.fail(new TransException(TransErrorCode.FAIL));
        }

        return Result.fail(ErrorPageConfig.TRANS_ERROR_CODE.get(code));
    }

    /**
     * 返回错误信息
     * 
     * @param code
     * @return
     * @throws Exception 
     */
    @RequestMapping("/json/500")
    @ResponseBody
    @ApiOperation("返回500错误信息")
    public Object toErrorJson500() throws Exception {
        return getMessage();
    }

    /**
     * 获取异常信息
     * 
     * @return
     */
    private Result<String> getMessage() {
        Throwable t = (Throwable)request.getAttribute(ConstantServlet.ERR_ATTRIBUTE_PARAM_NAME);

        if (t instanceof TransException) {
            response.setStatus(200);
            return Result.fail((TransException)t);
        }
        return Result.fail(t.getMessage());
    }

}